package com.example.project.projectspringcloud.utils;

import java.io.UnsupportedEncodingException;
import java.util.*;

public class PropertyUtil {

    private static Map<String, PropertyUtil> instance = Collections.synchronizedMap(new HashMap<String, PropertyUtil>());

    protected String sourceUrl;

    protected ResourceBundle resourceBundle;

    private static Map<String, String> convert = Collections.synchronizedMap(new HashMap<String, String>());

    protected PropertyUtil(String sourceUrl) {
        this.sourceUrl = sourceUrl;
        load();
    }

    public static PropertyUtil getInstance(String sourceUrl) {
        synchronized (PropertyUtil.class) {
            PropertyUtil manager = (PropertyUtil) instance.get(sourceUrl);
            if (manager == null) {
                manager = new PropertyUtil(sourceUrl);
                instance.put(sourceUrl, manager);
            }
            return manager;
        }
    }

    private synchronized void load() {
        try {
            this.resourceBundle = ResourceBundle.getBundle(this.sourceUrl);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("sourceUrl = " + this.sourceUrl + "file load error!", e);
        }
    }

    public String getProperty(String key) {
        try {
            return new String(this.resourceBundle.getString(key).getBytes("iso-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
        }
        return this.resourceBundle.getString(key);
    }

    public Map<String, String> readyConvert() {
        Enumeration<String> enu = this.resourceBundle.getKeys();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = this.resourceBundle.getString(key);
            convert.put(value, key);
        }
        return convert;
    }

    public Map<String, String> readyConvert(ResourceBundle resourcebundle) {
        Enumeration<String> enu = resourcebundle.getKeys();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = resourcebundle.getString(key);
            convert.put(value, key);
        }
        return convert;
    }
}
